package cn.itsource.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("*.session")
public class SessionServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//创建session的代码
		HttpSession session1 = req.getSession();
		HttpSession session2 = req.getSession();
		session1.setMaxInactiveInterval(30);//设置session的过期时间为30秒		
		System.out.println(session1 == session2);//true
		
		//在session中存放值
		session1.setAttribute("name", "汤姆");
		session1.setAttribute("age", 30);
		
		//在session对象中获取值
		System.out.println(session2.getAttribute("name"));//汤姆
		//session中移除值
		session1.removeAttribute("name");
		
		System.out.println(session2.getAttribute("name"));//null
	}
}
